﻿using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BenzInfo.Hockey.Grenadiers.Models;
using BenzInfo.Hockey.Grenadiers.Utils;

namespace BenzInfo.Hockey.Grenadiers.Controllers
{ 
    public class StaffController : BaseController
    {
        private GrenadiersContext db = new GrenadiersContext();

        private const string BASE_PATH = "~/Content/images/staff";

        //
        // GET: /Staff/

        public ViewResult Index()
        {
            return View(db.Staff.ToList());
        }

        //
        // GET: /Staff/Create

        public ActionResult Create()
        {
            return PartialView();
        } 

        //
        // POST: /Staff/Create

        [HttpPost]
        public ActionResult Create(Staff staff, HttpPostedFileBase inputFile)
        {
            if (inputFile != null)
            {
                using (Image img = Image.FromStream(inputFile.InputStream))
                {
                    string ext = inputFile.FileName.Split('.')[inputFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{1}_{0:000}.{2}", DateTime.Now.Ticks, staff.Id, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH), filename);

                    ImageHelper.Save(img, filepath, ext);

                    staff.Picture = filename;
                }
            }

            if (ModelState.IsValid)
            {
                staff.Position = db.Staff.Count(s => s.CategoryValue == staff.CategoryValue) + 1;

                db.Staff.Add(staff);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return PartialView(staff);
        }

        //
        // GET: /Staff/EditPosition

        public void EditPosition(int[] items)
        {
            int i = 1;
            foreach (int id in items)
            {
                Staff s = db.Staff.Find(id);
                s.Position = i++;
                db.Entry(s).State = EntityState.Modified;
            }

            db.SaveChanges();
        }
        
        //
        // GET: /Staff/Edit/5
 
        public ActionResult Edit(int id)
        {
            Staff staff = db.Staff.Find(id);
            return PartialView(staff);
        }
        
        //
        // POST: /Staff/Edit/5

        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(Staff staff, HttpPostedFileBase inputFile, string nopicture)
        {
            if (nopicture == "on")
            {
                staff.Picture = null;
            }
            else if (inputFile != null)
            {
                using (Image img = Image.FromStream(inputFile.InputStream))
                {
                    string ext = inputFile.FileName.Split('.')[inputFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{1}_{0:000}.{2}", DateTime.Now.Ticks, staff.Id, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH), filename);

                    ImageHelper.Save(img, filepath, ext);

                    staff.Picture = filename;
                }
            }

            if (ModelState.IsValid)
            {
                db.Entry(staff).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return PartialView(staff);
        }

        //
        // GET: /Staff/Delete/5
 
        public ActionResult Delete(int id)
        {
            Staff staff = db.Staff.Find(id);
            return PartialView(staff);
        }

        //
        // POST: /Staff/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Staff staff = db.Staff.Find(id);
            db.Staff.Remove(staff);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}